package com.jp.knowledge.view;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v4.content.LocalBroadcastManager;
import android.util.AttributeSet;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.ImageView;

import com.anarchy.classify.ClassifyView;
//import com.anarchy.classify.model.TiyanApps;
import com.jp.knowledge.model.experience.TiyanApps;
import com.jiayen.dialog.LoadingDialog;
import com.jiayen.util.ToasUtil;
import com.jp.knowledge.R;
import com.jp.knowledge.broad.LocalBroadCast;
import com.jp.knowledge.dialog.CommDialog;
import com.jp.knowledge.logic.TiyanLogic;
import com.jp.knowledge.model.IModel;


import java.util.List;

/**
 * Created by jiayen on 16/12/22.
 */

public class ItemMegreView extends ClassifyView {
    private Context mContext;

    private String lastGroupName;

    private List<TiyanApps> lastList;
    private int lastPostion;
    /***
     * 后续添加组名 与删除组icon
     */
    protected EditText groupName;
    protected ImageView delGroup;

    private CommDialog commDialog;

    private Dialog  subDialog;

    private boolean edit;//是否是编辑状态

    private TiyanLogic logic;
    private LoadingDialog loading;
    private LocalBroadcastManager localManager;

    public ItemMegreView(Context context) {
        this(context, null);
    }

    public ItemMegreView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ItemMegreView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        init();
    }

    public boolean isEdit() {
        return edit;
    }

    public void setEdit(boolean edit) {
        this.edit = edit;
        if(groupName != null)
        groupName.setBackgroundResource(R.color.gray_bg);
    }

    private void init() {
        localManager=LocalBroadcastManager.getInstance(mContext);
        loading = new LoadingDialog(mContext);
        logic = new TiyanLogic(mContext);


        logic.setDelGroupCallBack(new TiyanLogic.DelGroupCallBack() {
            @Override
            public void delCompelete(IModel data) {
                if(data.getErrcode()==0){
                    ToasUtil.toast(mContext,"删除成功");
                    loading.cancel();
                    subDialog.dismiss();
                    Intent intent = new Intent(LocalBroadCast.DEL_TIYAN_GROUP);
                    localManager.sendBroadcast(intent);

                }else{
                    ToasUtil.toast(mContext,"删除失败");
                    loading.cancel();
                }

            }
        });

        commDialog = new CommDialog(mContext);
        commDialog.setRight("确定");
        commDialog.setContent("你将删除该组?");
        commDialog.setRightListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                loading.show();
                logic.delTIyanGroup(lastList.get(0).getParentMid());
                commDialog.dismiss();
            }
        });
    }


    @Override
    protected Dialog createSubDialog() {
        Dialog dialog = new Dialog(mContext, R.style.custom_idalgo);
        dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        Window window = dialog.getWindow();
        WindowManager.LayoutParams layoutParams = window.getAttributes();
//        layoutParams.dimAmount = 0.6f;
//        layoutParams.windowAnimations = com.anarchy.classify.R.style.DefaultAnimation;
        window.setAttributes(layoutParams);
        dialog.setCancelable(true);

        dialog.setOnDismissListener(dismissListener);
        subDialog =dialog;
        return dialog;
    }


    private DialogInterface.OnDismissListener dismissListener = new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialog) {
            groupName.clearFocus();//失去焦点
            groupName.setFocusable(false);
            groupName.setFocusableInTouchMode(false);

            updateGroupName();
        }
    };

    /**
     * 更新  组名
     */
    private void updateGroupName() {
        try {
            String curGroupName = groupName.getText().toString().trim();

            if(curGroupName!=null&&!curGroupName.equals(lastGroupName)){
//                baseSimpleAdapter.updateItem(lastPostion,curGroupName);

                //调用接口更新名
                logic.groupRename(lastList.get(0).getGid(),curGroupName);
            }

        }catch (Exception e){

        }

    }

    @Override
    protected View getSubContent() {
        return inflate(getContext(), R.layout.custom_sub_content, null);
    }


//    @Override
    protected void getSubContentView(View content) {
        groupName = (EditText) content.findViewById(R.id.group_name);
        groupName.setFocusable(false);
        delGroup= (ImageView) content.findViewById(R.id.del_group_icon);
        groupName.setOnClickListener(goupClick);
        if(edit){
            groupName.setBackgroundResource(R.color.gray_bg);
        }else{
            groupName.setBackgroundResource(R.color.white);
        }


        delGroup.setOnClickListener(delGroupClick);
    }

    private OnClickListener  delGroupClick = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if(edit){
                commDialog.show();
            }else {
                subDialog.dismiss();
            }
        }
    };

    private OnClickListener goupClick = new OnClickListener() {
        @Override
        public void onClick(View v) {
            if(edit){
                groupName.setFocusable(true);
                groupName.setFocusableInTouchMode(true);
                groupName.requestFocus();
            }
        }
    };

//    @Override
    protected void setSubValue(int position, List list) {
        lastPostion = position;
        if(list!=null&&list.size()>0){
            TiyanApps  apps  = (TiyanApps) list.get(0);
            if(apps!=null&&groupName!=null) {
                groupName.setText(apps.getGName());
                lastGroupName = apps.getGName();
                lastList=list;
            }
        }
    }
}
